package com.github.easyms.server.controller.auth;

import com.github.easyms.common.base.BaseController;
import com.github.easyms.common.entity.auth.Menu;
import com.github.easyms.common.entity.auth.User;
import com.github.easyms.common.vo.PageParams;
import com.github.easyms.common.vo.ResultPage;
import com.github.easyms.common.vo.auth.ParentAuthMenuVo;
import com.github.easyms.server.service.auth.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单管理接口
 *
 * @author Leo.Liao
 * @date 2020-04-28 23:34:26
 */
@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/api/auth/menu")
public class MenuController extends BaseController<Menu> {

    @Resource
    private MenuService menuService;

    @Override
    public ResponseEntity<ResultPage<Menu>> list(PageParams pageParams, Menu menu) {
        service = menuService;
        return super.list(pageParams,menu);
    }

    @Override
    public ResponseEntity<Menu> insert(@RequestBody Menu entity) {
        service = menuService;
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        entity.setEditUser(currentUser.getUserId());
        return super.insert(entity);
    }

    @Override
    public ResponseEntity<Menu> update(@RequestBody Menu entity) {
        service = menuService;
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        entity.setEditUser(currentUser.getUserId());
        return super.update(entity);
    }

    @Override
    public ResponseEntity<Menu> delete(@RequestBody Menu entity) {
        service = menuService;
        return super.delete(entity);
    }

    @Override
    public ResponseEntity<List<Menu>> listCombobox(Menu entity,String q) {
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        return ResponseEntity.ok(menuService.listCombobox(entity,currentUser.getLanguage(),q));
    }

    @ApiOperation(value = "获取当前登陆用户权限菜单树")
    @GetMapping("/getUserAuthMenuTree")
    public ResponseEntity<List<ParentAuthMenuVo>> getUserAuthMenuTree(){
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        List<ParentAuthMenuVo> authMenuTree = menuService.getAuthMenuTree(currentUser.getRoleId(), currentUser.getLanguage());
        return ResponseEntity.ok(authMenuTree);
    }

    @ApiOperation(value = "获取权限菜单树")
    @GetMapping("/getCheckedAuthMenuTree")
    public ResponseEntity<List<ParentAuthMenuVo>> getCheckedAuthMenuTree(String roleId){
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        List<ParentAuthMenuVo> authMenuTree = menuService.getCheckedAuthMenuTree(roleId, currentUser.getLanguage());
        return ResponseEntity.ok(authMenuTree);
    }

}
